/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.common.web.taglib;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.log4j.Logger;
import org.apache.struts.taglib.TagUtils;


/**
 * Tag de seguridad
 * 
 * @author nicolas.gonzalez
 */
@SuppressWarnings("serial")
public class SecurityTag extends SimpleTagSupport {
    
    /** Logger */
    private static Logger logger = Logger.getLogger(SecurityTag.class);  
    
    /** Roles separados por ',' */
    private String roles;

    /** @see TagUtils#lookup(javax.servlet.jsp.PageContext, java.lang.String, java.lang.String) */
    protected Object lookup(PageContext pageContext, String name, String scopeName) throws JspException {
        return TagUtils.getInstance().lookup(pageContext, name, scopeName);
    }

    /** @see TagUtils#lookup(PageContext, String, String, String) */
    protected Object lookup(PageContext pageContext, String name, String propery, String scopeName) throws JspException {
        return TagUtils.getInstance().lookup(pageContext, name, propery, scopeName);
    }
    
    
    /** @see TagUtils#write(javax.servlet.jsp.PageContext, java.lang.String) */
    protected void write(PageContext pageContext, String text) throws JspException {
        TagUtils.getInstance().write(pageContext, text);
    }

    /** @see TagUtils#write(javax.servlet.jsp.PageContext, java.lang.String) */
    protected void write(PageContext pageContext, StringBuffer text) throws JspException {
        TagUtils.getInstance().write(pageContext, text.toString());
    }


    /**
     * Genera el codigo html al tag correspondiente de acuerdo a la
     * configuracion
     */
    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext = (PageContext) this.getJspContext();

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        StringTokenizer token = new StringTokenizer(this.roles, ",");
        while (token.hasMoreElements()) {
            String role = token.nextToken().trim();
            
            if (request.isUserInRole(role)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Usuario con rol: " + role + ", tiene acceso.");
                }
                this.getJspBody().invoke(getJspContext().getOut());
                break;
            }
        }
    }
    
    /**
     * @param roles the roles to set
     */
    public void setRoles(String roles) {
        this.roles = roles;
    }
    
}
